package com.cndw.kungfu.business.gm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.kungfu.comm.AccessClose;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.comm.LocalCommand;
import com.cndw.kungfu.domain.Event;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.LogPlayer;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.GoodsManager;
import com.cndw.kungfu.model.goods.PlayerBoxs;
import com.cndw.kungfu.model.pvp.war.WarConfig;
import com.cndw.kungfu.model.sys.LogManager;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 任务处理接口
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-6-8 上午11:14:54
 */
@Controller
public class BusinessGoods {
	@Autowired
	private GoodsManager goodsManager;
	@Autowired
	private LogManager logManager;

	@UnAuth
	@LocalCommand
	public ResponseMessage giveGoods(Session session, String sign, String accountName, int goodsId, int num) {
		long playerId = Context.getPlayerServer().getPlayerIdByAccount(accountName);
		GoodsMode goodsMode = Context.getGoodsServer().getModeById(goodsId);
		if (playerId > 0 && goodsMode != null) {
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			int maxSize = player.getPlayerBoxs().getBlackNumbers() * goodsMode.getMaxStack();
			Context.getGoodsServer().giveGoods(playerId, goodsId, NumberProcesser.between(num, 1, maxSize));
			return ResponseMessage.getSuccMessage(true);
		}
		return ResponseMessage.getSuccMessage(false);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage getGoods(Session session, String sign) {

		Collection<GoodsMode> goodsList = Context.getCacheCenter().getGoodsMode();
		if (!goodsList.isEmpty()) {
			final Integer equipType = new Integer(1); // 装备
			final Integer gemType = new Integer(2); // 宝石
			final Integer bookType = new Integer(3); // 技能书
			final Integer drugType = new Integer(4); // 药品
			final Integer groceriesType = new Integer(5); // 杂货
			Map<Integer, List<String[]>> map = new HashMap<Integer, List<String[]>>();
			for (GoodsMode mode : goodsList) {

				Integer type = null;
				if (mode.getRootId() == 2) {
					type = equipType;
				} else if (mode.getGoodTypeId() == 168 || mode.getGoodTypeId() == 104 || mode.getGoodTypeId() == 105) {
					type = gemType;
				} else if (mode.getGoodTypeId() == 102 || mode.getGoodTypeId() == 106) {
					type = bookType;
				} else if (mode.getGoodTypeId() == 101) {
					type = drugType;
				} else if (mode.getGoodTypeId() == 188 || mode.getGoodTypeId() == 103 || mode.getGoodTypeId() == 107) {
					type = groceriesType;
				} else {
					continue;
				}

				if (!map.containsKey(type)) {
					map.put(type, new ArrayList<String[]>());
				}

				map.get(type).add(new String[] { String.valueOf(mode.getGoodsId()), mode.getName() });
			}
			return ResponseMessage.getSuccMessage(map);
		}

		return ResponseMessage.getSuccMessage(null);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage fixedGoods(Session session,String sign, long uid, int id) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getFailMessage(-1);
		}
		PlayerBoxs targetBoxs = target.getPlayerBoxs();
		Collection<Goods> allGoods = targetBoxs.getAllGoods();
		for (Goods goods : allGoods) {
			if (goods.getId() == id) {
				return ResponseMessage.getFailMessage(-2);
			}
		}
		Goods goods = goodsManager.selectOne(uid, id);
		if (goods == null) {
			return ResponseMessage.getFailMessage(-3);
		}
		goods.setStoragePlace(0);
		goods.setStorageType(0);
		targetBoxs.add(goods);		

		return ResponseMessage.getSuccMessage(1);
	}
	
	public ResponseMessage fixedGoodsAll(Session session,String sign,long uid){
	
		
		return ResponseMessage.getSuccMessage(1);
	}
	
	
	@UnAuth
	@GmCommand
	@AccessClose
	public ResponseMessage clearBug(Session session,String sign){
		int beginTime = TimeProcesser.getUnixTimeSilent("2012-08-07 06:00:00");
		int endTime = TimeProcesser.getUnixTime();
		List<LogPlayer> allList = logManager.getLogsAll(90303, beginTime, endTime);
		String title = LanguageLib.get("war.award.title");
		StringBuilder sb = new StringBuilder();
		if (!allList.isEmpty()) {
			for (LogPlayer e : allList) {
				try {
					Event event = logManager.getEvent(e.getPlayerId(), title, beginTime);
					
					if (event == null) {
						LogUtil.getMain().debug("not event");
						continue;
					}else {
						logManager.delEvent(e.getPlayerId(), event.getId());
					}
					
					
					String[] xxx = e.getParams().split("\\$@");
					
					int gNum = Integer.valueOf(xxx[0]);//英雄令
					int aNum = Integer.valueOf(xxx[1]);//银两
					if (gNum>0 || aNum>0) {
						LogUtil.getMain().debug("war xxxx "+e.getPlayerId() +" g:"+gNum +" a:"+aNum);
						
					}
					

					
					Player target = Context.getPlayerServer().getByPlayerId(e.getPlayerId());
					if (target == null) {
						continue;
					}
					target.alterAsset(-aNum);
					target.commitSync();
					target.getPlayerBoxs().costGoods(WarConfig.heroGoodsId, gNum);
					
					LogUtil.getMain().debug("war debug "+e.getPlayerId() +" g:"+gNum +" a:"+aNum);
					
				} catch (Exception e2) {
					e2.printStackTrace();
				}

				
				
				
				sb.append("id :"+e.getPlayerId());
				sb.append("c: "+e.getParams());
				sb.append("t :"+TimeProcesser.getDateByUnixTime(e.getAddTime()));
				sb.append("----");
			}
			return ResponseMessage.getSuccMessage(sb.toString());
		}
		return ResponseMessage.getSuccMessage(1);
	}

}
